package com.bdmc.smokesensor.dao;

import java.io.Serializable;
//import java.util.List;
import java.util.List;

import com.bdmc.smokesensor.entity.Rbac_Role;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

public interface Rbac_RoleJPA
        extends JpaRepository<Rbac_Role, Integer>, JpaSpecificationExecutor<Rbac_Role>, Serializable {
    // public List<Rbac_Role> findByparentFunIdOrderByFunOrderAsc(int pid);
    @Query(value = "select f.Id as id,f.ParentFunId as Pid,f.FunName as title,cast(N.checked as SIGNED) as checked,"
            + " f.Id as funsort, cast(0 as SIGNED) as funtype from rbac_function f left join "
            + " (select b.*, 1 checked from rbac_function b where "
            + " FIND_IN_SET(b.id,(select StrFuction from rbac_role where Id = :rid))) N on f.Id = N.Id ", nativeQuery = true)
    public List<Object[]> GetzTreeListByRoleId(int rid);

    @Query(value = "SELECT  a1.id as id,a1.rbacFunctionId as Pid,a1.actionName as title,a2.checked,a1.id as funsort,"
            + "1 as funtype FROM rbac_action a1 left join (SELECT *,1 checked from rbac_action ac WHERE"
            + " FIND_IN_SET(ac.id,(SELECT strAction from rbac_role where id=:rid))) a2"
            + " on a1.id=a2.id", nativeQuery = true)
    public List<Object[]> GetzActionListByRoleId(int rid);

    @Query(value="select r from Rbac_Role r left join Sys_Department d on r.departMentId = d.id where d.id=:dep_id or d.treeRoot like %:treeRoot%" )
    public List<Rbac_Role> findBytreeRoot(int dep_id,String treeRoot);
}
